package com.ycrlkj.android.modulespublic.common.widget.titlebar;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ycrlkj.android.modulesbase.libbase.utils.Logger;
import com.ycrlkj.android.modulespublic.common.R;

@SuppressLint("AppCompatCustomView")
public class MTitleBar extends RelativeLayout {

    private BtnEvents btnEvents;

    private TextView btnLeft;
    private TextView btnRight;
    private TextView textViewTitle;
    private TypedArray typedArray;

    @SuppressLint("Recycle")
    public MTitleBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.m_title_bar, this, true);
        initView(context, attrs);
    }

    public void initView(Context context, AttributeSet attrs) {

        //获取 attrs.xml 文件里 MTitleBar 属性组的所有属性
        typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.MTitleBar);

        btnLeft = findViewById(R.id.m_title_bar_btn_left);
        btnRight = findViewById(R.id.m_title_bar_btn_right);
        textViewTitle = findViewById(R.id.m_title_bar_title);

        //初始化标题
        String titleText = typedArray.getString(R.styleable.MTitleBar_title_text);
        textViewTitle.setText(titleText);

        //初始化左侧按钮 注意: 此属性如果在自定义控件属性未传参, 则设置默认值为true
        boolean btnLeftVisible = typedArray.getBoolean(R.styleable.MTitleBar_left_button_visible, true);
        initLeftBtn(btnLeftVisible);

        //初始化右侧按钮 注意: 此属性如果在自定义控件属性未传参, 则设置默认值为true
        boolean btnRightVisible = typedArray.getBoolean(R.styleable.MTitleBar_right_button_visible, true);
        initRightBtn(btnRightVisible);
    }

    public void initLeftBtn(boolean visible) {
        if (!visible) {
            //如果不显示左边按钮
            btnLeft.setVisibility(View.INVISIBLE);
        } else {
            //如果显示左边按钮
            btnLeft.setVisibility(View.VISIBLE);
            String btnLeftText = typedArray.getString(R.styleable.MTitleBar_left_button_text);
            btnLeft.setText(btnLeftText);

            //绑定事件
            btnLeft.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (btnEvents != null) {
                        btnEvents.setBtnLeftOnClickListener();
                    }
                }
            });

            if (typedArray.hasValue(R.styleable.MTitleBar_left_button_text_size)) {
                //设置左边按钮文字大小, 如果未传参, 则默认为14
                btnLeft.setTextSize(TypedValue.COMPLEX_UNIT_PX, typedArray.getDimensionPixelSize(R.styleable.MTitleBar_left_button_text_size, sp2px(getContext()/*设定的dp值必须在此处转为px才有效*/, 14)));
            }
        }
    }

    public void initRightBtn(boolean visible) {

        if (!visible) {
            //如果不显示右边按钮
            btnRight.setVisibility(View.INVISIBLE);
        } else {
            //如果显示右边按钮
            btnRight.setVisibility(View.VISIBLE);
            String btnRightText = typedArray.getString(R.styleable.MTitleBar_right_button_text);
            btnRight.setText(btnRightText);

            //绑定事件
            btnRight.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (btnEvents != null) {
                        btnEvents.setBtnRightOnClickListener();
                    }
                }
            });

            if (typedArray.hasValue(R.styleable.MTitleBar_right_button_text_size)) {
                //设置右边按钮文字大小, 如果未传参, 则默认为14
                btnRight.setTextSize(TypedValue.COMPLEX_UNIT_PX, typedArray.getDimensionPixelSize(R.styleable.MTitleBar_right_button_text_size, sp2px(getContext()/*设定的dp值必须在此处转为px才有效*/, 14)));
            }
        }
    }

    public void setTitle(String title) {
        textViewTitle.setText(title);
    }

    public void setBtnRightText(String text) {
        btnRight.setText(text);
    }

    public void setBtnLeftVisibility(boolean visible) {
        initLeftBtn(visible);
    }

    public void setBtnRightVisibility(boolean visible) {
        initRightBtn(visible);
    }

    public interface BtnEvents {
        void setBtnLeftOnClickListener();

        void setBtnRightOnClickListener();
    }

    public void setBtnEvents(BtnEvents btnEvents) {
        this.btnEvents = btnEvents;
    }

    /**
     * dp转px
     */
    static int dp2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     * sp转px
     */
    static int sp2px(Context context, float spValue) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
    }

}